package com.easyedu.dean.service;

import com.easyedu.core.entity.DbError;
import com.easyedu.core.entity.DeanServiceError;
import com.easyedu.core.entity.po.Institute;
import com.easyedu.core.entity.BaseError;
import com.easyedu.core.exception.EasyEduException;
import com.easyedu.core.dao.InstituteDao;
import com.easyedu.dean.entity.dto.request.ReqInstitute;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lmr
 **/
@Service
public class InstituteService {

    private final InstituteDao instituteDao;

    public InstituteService(InstituteDao instituteDao) {
        this.instituteDao = instituteDao;
    }

    public void createInstitute(ReqInstitute reqInstitute) {
        Institute institute=new Institute(reqInstitute.getInstituteId(),reqInstitute.getInstituteName(),reqInstitute.getDepartmentId(),reqInstitute.getDepartmentName());
        Integer integer = instituteDao.createInstitute(institute);
        if (integer != 1) {
            throw new EasyEduException(DbError.Err_INSERT_DB_ERROR, "新增学院信息失败");
        }
    }

    public void deleteInstitute(ReqInstitute reqInstitute) {
        Institute institute=new Institute(reqInstitute.getInstituteId(),reqInstitute.getInstituteName(),reqInstitute.getDepartmentId(),reqInstitute.getDepartmentName());
        Integer integer = instituteDao.deleteInstitute(institute);
        if (integer != 1) {
            throw new EasyEduException(DbError.Err_DELETE_DB_ERROR, "删除学院信息失败");
        }
    }

    public boolean hasInstitute(long openInstituteId) {
        Institute instituteById = instituteDao.getInstituteById(openInstituteId);
        return instituteById != null;
    }

    public boolean hasInstitute(long openInstituteId, String openInstituteName) {
        Institute instituteById = instituteDao.getInstituteById(openInstituteId);
        if (instituteById == null) {
            throw new EasyEduException(DeanServiceError.ERR_INSTITUTE_NOT_EXIST);
        }
        if (!instituteById.getInstituteName().equals(openInstituteName)) {
            throw new EasyEduException(BaseError.ERR_PARA, "所输入的学院id与学院名称不匹配");
        }
        return true;
    }

    public boolean hasDepartment(long openInstituteId, long openDepartmentId,
                                 String openDepartmentName) {
        Institute instituteById = instituteDao.getDepartmentById(openInstituteId, openDepartmentId);
        if (instituteById == null) {
            throw new EasyEduException(DeanServiceError.ERR_DEPARTMENT_NOT_EXIST);
        }
        if (!instituteById.getDepartmentName().equals(openDepartmentName)) {
            throw new EasyEduException(BaseError.ERR_PARA, "所输入的专业id与专业名称不匹配");
        }
        return true;
    }

    public List<Institute> getInstitute() {
        return instituteDao.getInstitute();
    }
}
